package com.xh.web.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.*;
import com.xh.web.domain.vo.ImportColumnVo;
import com.xh.web.domain.vo.ProjectVo;
import com.xh.web.exception.CustomException;
import com.xh.web.mapper.*;
import com.xh.web.service.IDProjectService;
import com.xh.web.util.*;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 项目Service业务层处理
 *
 * @author dongxuefei
 * @date 2020-09-17
 */
@Service
public class DProjectServiceImpl implements IDProjectService {

    @Autowired
    private DProjectMapper dProjectMapper;

    @Autowired
    private DProjectAuthMapper dProjectAuthMapper;

    @Autowired
    private DTableImportMapper dTableImportMapper;
    @Autowired
    private DColumnImportMapper dColumnImportMapper;
    @Autowired
    private DImportMapper dImportMapper;
    @Autowired
    private DTableMapper dTableMapper;
    @Autowired
    private DColumnMapper dColumnMapper;
    @Autowired
    private DProjectHisMapper dProjectHisMapper;

    @Autowired
    private DTableIndexMapper dTableIndexMapper;



    /**
     * 查询项目
     *
     * @param id 项目ID
     * @return 项目
     */
    @Override
    public DProject selectDProjectById(String id) {
        return dProjectMapper.selectDProjectById(id);
    }

    /**
     * 查询首页项目
     *
     * @return 项目
     */
    public List<ProjectVo> selectAllProject(DProject dProject) {
        return dProjectMapper.selectAllProject(dProject);
    }

    /**
     * 查询项目列表
     *
     * @param dProject 项目
     * @return 项目
     */
    @Override
    public List<DProject> selectDProjectList(DProject dProject) {
        return dProjectMapper.selectDProjectList(dProject);
    }

    /**
     * 新增项目
     *
     * @param dProject 项目
     * @return 结果
     */
    @Override
    public int insertDProject(DProject dProject) {
        if (dProject == null) {
            return 0;
        }
        if(StringUtils.isNotNull(dProject.getId())){

            dProject.setUpdateName("admin");
            dProject.setUpdateBy("admin");
            dProject.setUpdateDate(new Date());
            return dProjectMapper.updateDProject(dProject);
        }
        int count = dProjectMapper.selectProjectCount(1);
        String str = String.format("%04d", count + 1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        dProject.setProjectCode("D" + dateFormat.format(new Date()) + str);
        dProject.setOwnerId(1);
        dProject.setCreateName("admin");
        dProject.setUpdateName("admin");
        dProject.setCreateBy("admin");
        dProject.setCreateDate(new Date());
        dProject.setUpdateBy("admin");
        dProject.setUpdateDate(new Date());
        dProject.setDelFlag("0");
        return dProjectMapper.insertDProject(dProject);
    }


    /**
     * 修改项目
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateDProject(String id, DProject dProject) {

        DProject dProject1 = dProjectMapper.selectDProjectById(id);

        if (dProject == null) {
            return 0;
        }
        dProject1.setProjectName(dProject.getProjectName());
        dProject1.setDbType(dProject.getDbType());
        dProject1.setRemarks(dProject.getRemarks());
        dProject1.setUpdateName("admin");
        dProject1.setUpdateBy("admin");
        dProject1.setUpdateDate(new Date());
        return dProjectMapper.updateDProject(dProject1);
    }


    /**
     * 删除项目信息
     *
     * @param id 项目ID
     * @return 结果
     */
    @Override
    public int deleteDProjectById(String id) {

        DProject dProject1 = dProjectMapper.selectDProjectById(id);
        if (id == null) {
            return 0;
        }
        return dProjectMapper.deleteDProjectById(id);
    }






    /*
     * 查询所有组员
     *
     * */
    @Override
    public List<DProjectAuth> selectMembers(String id) {
        return dProjectAuthMapper.selectDProjectAuthList(id);
    }


    /*
     * 删除组员
     *
     * */
    @Override
    public int deleteAuthById(String id) {
        return dProjectAuthMapper.deleteAuthById(id);
    }

    @Override
    public DProject selectDProjectByCode(String projectCode) {
        return null;
    }

    @Override
    @Transactional
    public AjaxResult importProject(String importJson) {
        JSONObject jsonImport = JsonUtil.parseObject(importJson, JSONObject.class);
        String userName = jsonImport.get("userName").toString();
        if (StringUtils.isEmpty(userName)) {
            return AjaxResult.error("平台用户不能为空,导入失败");
        }
        String password = jsonImport.get("password").toString();
        if (StringUtils.isEmpty(password)) {
            return AjaxResult.error("平台密码不能为空,导入失败");
        }

        JSONArray importTableArray = jsonImport.getJSONArray("ImportTable");
        //导入记录
        String importId = "111";
        DImport dImport = new DImport();
        //给导入记录表 赋值
        importLog(importTableArray.size(), importId, dImport);
        String dbType = jsonImport.get("dbType").toString();
        String inportType = jsonImport.get("inportType").toString();//导入类型
        String projectId=null;//项目ID
       // String projectCode = jsonImport.get("projectCode").toString();
        if(inportType!=null&&inportType.equals("1")){
            String projectCode = jsonImport.get("projectCode").toString();
            if(StringUtils.isNull(projectCode)){
                return AjaxResult.error("数据库编码不能为空");
            }
            DProject project = new DProject();
            project.setProjectCode(projectCode);
            project.setOwnerId(1);
            List<ProjectVo> dProjects = dProjectMapper.selectAllProject(project);
            if (dProjects.isEmpty()) {
                return AjaxResult.error("数据库未查询到,请检查数据库编码是否正确");
            }
            DProject dProject = dProjects.get(0);
            String tableCode=null;
            if(jsonImport.get("tableCode")!=null){
                tableCode=jsonImport.get("tableCode").toString();
            }
            dImport.setProjectCode(dProject.getProjectCode());
            dImport.setProjectName(dProject.getProjectName());
            dImport.setProjectId(dProject.getId());
            dImport.setImportMode("客户端导入");
            dImport.setTableNum(importTableArray.size());

            dProject.setTableNum(dTableMapper.selectTableNumByProjectId(dProject.getId()));
            dProject.setUpdateName("admin");
            dProject.setUpdateDate(DateUtils.getNowDate());
            dProject.setUpdateBy("admin");
            dProjectMapper.updateDProject(dProject);
            projectId=dProject.getId();
            //删除项目下所有的表和列
            dTableMapper.deleteAllTableByProjectId(projectId,tableCode);
            dColumnMapper.deleteAllColumnByProjectId(projectId,tableCode);
            //新写：删除项目下所有的索引
            dTableIndexMapper.deleteAllIndexByProjectId(projectId,tableCode);
        }else{
            //新增项目获取项目的id
            projectId = insertProjectImport(dImport, dbType, importTableArray.size());
        }
        for (int i = 0; i < importTableArray.size(); i++) {
            JSONObject tableJson = importTableArray.getJSONObject(i);
            //表注释
            String tableName = tableJson.get("tableName").toString();
            String tableCode = tableJson.get("tableCode").toString();
            //表的列名
            JSONArray importColumnsArray = tableJson.getJSONArray("importColumns");
            //表的索引
            JSONArray importIndexArray = jsonImport.getJSONArray("ImportIndex");
            //新增表  表的id是需要返回的
            String tableId = insertTableImport(projectId, tableName, tableCode, importColumnsArray.size());

            for (int j = 0; j < importColumnsArray.size(); j++) {
                JSONObject ColumnsJson = importColumnsArray.getJSONObject(j);
                //直接转列 赋值到准备好的实体类
                ImportColumnVo userEntities = JsonUtil.parseObject(ColumnsJson.toString(), ImportColumnVo.class);
                //字段没有注释 不懂去看一下数据库表
                DColumn dColumn = new DColumn();
                insertColumnImport(projectId, tableId, userEntities, dColumn,tableCode);
                dColumnMapper.insertDColumn(dColumn);
            }

            for (int k = 0; k < importIndexArray.size(); k++) {
                JSONObject IndexsJson = importIndexArray.getJSONObject(k);
              DTableIndex dTableIndex=  new DTableIndex();
                //表注释
                String indexName = IndexsJson.get("indexName").toString();
                String indexType = IndexsJson.get("indexType").toString();
                String indexColumn = IndexsJson.get("indexColumns").toString();
               insertIndexImport(projectId, indexName,tableCode,tableName,dTableIndex,indexColumn,indexType);
            }

        }
        //1已导入
        dImport.setInportStatus("1");
        //导入记录表添加记录
        dImportMapper.insertDImport(dImport);
        //保存历史
        DProjectHis dProjectHis=new DProjectHis();
        dProjectHis.setProjectId(projectId);
        dProjectHis.setOperationType("2");
        if(importTableArray.size()==1){
            JSONObject tableJson = importTableArray.getJSONObject(0);
            String tableName = tableJson.get("tableName").toString();
            String tableCode = tableJson.get("tableCode").toString();
            dProjectHis.setTableCode(tableCode);
            dProjectHis.setTableName(tableName);
            dProjectHis.setOperationDesc(("导入表:"+tableCode));
        }else {
            dProjectHis.setOperationDesc("初始化导入");
        }
        dProjectHis.setCreateBy("admin");
        dProjectHis.setCreateName("admin");
        dProjectHis.setCreateDate(new Date());
        dProjectHis.setUpdateBy("admin");
        dProjectHis.setUpdateDate(new Date());
        dProjectHis.setDelFlag("0");
        dProjectHisMapper.insertDProjectHis(dProjectHis);
        return AjaxResult.success("平台无项目：批量插入成功");
    }


    @Override
    @Transactional
    public AjaxResult updateOrInsertProject(String id) {
        //查询记录表  获取项目code 根据项目code 查询出项目的id
        DImport dImport = dImportMapper.selectDImportById(id);
        DProject dProject = dProjectMapper.selectDProjectByCode(dImport.getProjectCode());
        if (dProject == null) {
            return AjaxResult.error("导入失败,平台没有这个数据库");
        }
        //根据项目id  查询出表的id
        DTableImport dTableImport = new DTableImport();
        dTableImport.setProjectId(dProject.getId());
        dTableImport.setDelFlag("0");
        List<DTableImport> dTableImports = dTableImportMapper.selectDTableImportList(dTableImport);
        //根据项目id 和 表名 查询 DTable 表看看是否有数据 如果有  就更新 没有就添加
        for (DTableImport tableImport : dTableImports) {
            DTable dTable = new DTable();
            dTable.setDelFlag("0");
            dTable.setProjectId(dProject.getId());
            dTable.setTableCode(tableImport.getTableCode());
            List<DTable> dTables = dTableMapper.selectDTableList(dTable);
            //由于字段都是一样的 可以直接拷贝
            BeanUtils.copyProperties(tableImport, dTable);
            //没有就新增
            if (dTables.isEmpty()) {
                dTable.setId(IdUtils.fastUUID());
                dTableMapper.insertDTable(dTable);
            } else {
                //有就修改
                dTable.setId(dTables.get(0).getId());
                dTableMapper.updateDTable(dTable);
            }
        }
        //根据表的id 和项目的id 查询出列
        for (DTableImport tableImport : dTableImports) {
            //表的id就涉及到循环
            DColumnImport dColumnImport = new DColumnImport();
            dColumnImport.setDelFlag("0");
            dColumnImport.setProjectId(dProject.getId());
            dColumnImport.setTableId(tableImport.getId());
            List<DColumnImport> dColumnImports = dColumnImportMapper.selectDColumnImportList(dColumnImport);
            for (DColumnImport columnImport : dColumnImports) {
                DColumn dColumn = new DColumn();
                dColumn.setDelFlag("0");
                dColumn.setProjectId(columnImport.getProjectId());
                dColumn.setTableId(columnImport.getId());
                List<DColumn> dColumnList = dColumnMapper.selectDColumnList(dColumn);
                //没有就新增
                BeanUtils.copyProperties(columnImport, dColumn);
                if (dColumnList.isEmpty()) {
                    dColumnMapper.insertDColumn(dColumn);
                } else {
                    dColumn.setId(dColumnList.get(0).getId());
                    dColumnMapper.updateDColumn(dColumn);
                }
            }




        }
        //删除两张临时表的数据临时表的数据
        DColumnImport dColumnImport = new DColumnImport();
        dColumnImport.setDelFlag("0");
        dColumnImport.setProjectId(dProject.getId());
        List<DColumnImport> dColumnImports = dColumnImportMapper.selectDColumnImportList(dColumnImport);
        for (DColumnImport columnImport : dColumnImports) {
            //逻辑删除 0  是 未删除 1  是删除
            columnImport.setDelFlag("1");
            dColumnImportMapper.updateDColumnImport(columnImport);
        }
        for (DTableImport deleteTableImport : dTableImports) {
            deleteTableImport.setDelFlag("1");
            dTableImportMapper.updateDTableImport(deleteTableImport);
        }
        //修改导入状态
        dImport.setInportStatus("1");
        dImport.setImportMode("导入已有项目");
        dImportMapper.updateDImport(dImport);

        return AjaxResult.success("导入成功");

    }

    /**
     * 添加导入记录表
     * @param importCount
     * @param importId
     * @param dImport
     */
    private void importLog(int importCount, String importId, DImport dImport) {
        dImport.setId(importId);
        dImport.setTableNum(importCount);
        dImport.setCreateBy("admin");
        dImport.setCreateName("admin");
        dImport.setCreateDate(new Date());
        dImport.setUpdateBy("admin");
        dImport.setUpdateDate(DateUtils.getNowDate());
        dImport.setDelFlag("0");
    }

    /**
     * 临时表 列
     * @param importId
     * @param dProject
     * @param dTableId
     * @param userEntities
     * @param dColumnImport
     */
    private void insertDColumnImportTemp(String importId, DProject dProject, String dTableId, ImportColumnVo userEntities, DColumnImport dColumnImport) {
        dColumnImport.setId(IdUtils.fastUUID());
        dColumnImport.setProjectId(dProject.getId());
        dColumnImport.setTableId(dTableId);
        dColumnImport.setColumnCode(userEntities.getColumnCode());
        dColumnImport.setColumnName(userEntities.getColumnName());
        dColumnImport.setDataType(userEntities.getDataType());
        dColumnImport.setdLength(userEntities.getdLength());
        dColumnImport.setIsEmpty(userEntities.getIsEmpty());
        dColumnImport.setIsPrimarykey(userEntities.getIsPrimarykey());
        dColumnImport.setDefaultValue(userEntities.getDefaultValue());
        dColumnImport.setCreateBy("admin");
        dColumnImport.setCreateDate(new Date());
        dColumnImport.setUpdateBy("admin");
        dColumnImport.setUpdateDate(new Date());
        dColumnImport.setDelFlag("0");
        dColumnImport.setdSort(1L);
        dColumnImport.setImportId(importId);
    }

    /**
     * 临时表  表
     * @param importId
     * @param dProject
     * @param tableComment
     * @param tableName
     * @param columnNum
     * @return
     */
    private String insertDTableImportTemp(String importId, DProject dProject, String tableComment, String tableName, int columnNum) {
        DTableImport dTableImport = new DTableImport();
        String fastUUID = IdUtils.fastUUID();
        dTableImport.setId(fastUUID);
        dTableImport.setProjectId(dProject.getId());
        dTableImport.setTableCode(tableName);
        dTableImport.setTableName(tableComment);
        dTableImport.setColumnNum(new Long(columnNum));
        dTableImport.setContentId("");
        dTableImport.setdSort(1L);
        dTableImport.setCreateBy("admin");
        dTableImport.setCreateDate(DateUtils.getNowDate());
        dTableImport.setUpdateBy("admin");
        dTableImport.setUpdateDate(DateUtils.getNowDate());
        dTableImport.setImportId(importId);
        dTableImport.setDelFlag("0");
        dTableImportMapper.insertDTableImport(dTableImport);
        return fastUUID;
    }

    /**
     * 数据表 列
     *
     * @param projectId
     * @param tableId
     * @param userEntities
     * @param dColumn
     */
    private void insertColumnImport(String projectId, String tableId, ImportColumnVo userEntities, DColumn dColumn,String tableCode) {
        //dColumn.setId(IdUtils.fastUUID());
        dColumn.setProjectId(projectId);
        dColumn.setTableId(tableId);
        dColumn.setTableCode(tableCode);
        dColumn.setColumnCode(userEntities.getColumnCode().toLowerCase());
        dColumn.setColumnName(userEntities.getColumnName());
        dColumn.setDataType(userEntities.getDataType().toLowerCase());
        if(!GenUtils.arraysContains(GenConstants.COLUMNTYPE_TIME, dColumn.getDataType())){
            dColumn.setdLength(userEntities.getdLength());
        }
        //dColumn.setdLength(userEntities.getdLength());
        dColumn.setIsEmpty(userEntities.getIsEmpty());
        dColumn.setIsPrimarykey(userEntities.getIsPrimarykey());
        dColumn.setDefaultValue(userEntities.getDefaultValue());
        dColumn.setCreateBy("admin");
        dColumn.setCreateDate(new Date());
        dColumn.setUpdateBy("admin");
        dColumn.setUpdateDate(new Date());
        dColumn.setDelFlag("0");
    }

    /**
     * 新增索引
     * @param
     * @return
     */
    private void insertIndexImport(String projectId, String indexName,String tableCode,String tableName,DTableIndex dTableIndex,String indexColumn,String indexType) {
        dTableIndex.setProjectId(projectId);
        dTableIndex.setIndexName(indexName);
        dTableIndex.setIndexType(indexType);
        dTableIndex.setIndexColumn(indexColumn);
        dTableIndex.setTableCode(tableCode);
        dTableIndex.setTableName(tableName);
        dTableIndex.setCreateBy("admin");
        dTableIndex.setCreateDate(new Date());
        dTableIndex.setUpdateBy("admin");
        dTableIndex.setUpdateDate(new Date());
        dTableIndex.setDelFlag("0");
        dTableIndex.setIndexCtype("1");
        dTableIndexMapper.insertDTableIndex(dTableIndex);
    }

    /**
     * 数据表   表
     *
     * @param projectId
     * @param tableComment
     * @param tableName
     * @return
     */
    private String insertTableImport(String projectId, String tableComment, String tableName, int columnNum) {
        DTable dTable = new DTable();
        //String fastUUID = IdUtils.fastUUID();
        //dTable.setId(fastUUID);
        dTable.setProjectId(projectId);
        dTable.setTableCode(tableName);
        dTable.setTableName(tableComment);
        dTable.setColumnNum(new Long(columnNum));
        dTable.setContentId("");
        //dTable.setdSort(1L);
        dTable.setCreateName("admin");
        dTable.setUpdateName("admin");
        dTable.setCreateBy("admin");
        dTable.setCreateDate(DateUtils.getNowDate());
        dTable.setUpdateBy("admin");
        dTable.setUpdateDate(DateUtils.getNowDate());
        dTable.setDelFlag("0");

        dTableMapper.insertDTable(dTable);
        //表的id是需要返回的
        return dTable.getId();
    }

    /**
     * 新增项目
     * @param dImport
     * @return
     */
    private String insertProjectImport(DImport dImport, String dbType, int tableNum) {
        //生成的uuid 当做项目的project id
        //String uuid = IdUtils.fastUUID();
        DProject dProject = new DProject();
        int count = dProjectMapper.selectProjectCount(1);
        String str = String.format("%04d", count + 1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        //dProject.setId(uuid);
        dProject.setProjectCode("P" + dateFormat.format(new Date()) + str);
        dProject.setProjectName("新建项目：" + str);
        dProject.setRemarks("数据库导入");
        dProject.setTableNum(tableNum);
        dProject.setDbType(dbType);
        dProject.setOwnerId(1);
        dProject.setCreateName("admin");
        dProject.setUpdateName("admin");
        dProject.setCreateBy("admin");
        dProject.setCreateDate(new Date());
        dProject.setUpdateBy("admin");
        dProject.setUpdateDate(new Date());
        dProject.setDelFlag("0");
        //导入记录表
        dImport.setProjectCode(dProject.getProjectCode());
        dImport.setProjectName(dProject.getProjectName());
        dProjectMapper.insertDProject(dProject);
        //新增成功获取项目的id
        return dProject.getId();
    }
    //更新创建人，修改人信息
    public int updateUser(DProject dProject){
        return dProjectMapper.updateProjectUser(dProject);
    }

    public void syncInportDb(String importId,MultipartFile file){
        DImport dImport=dImportMapper.selectDImportById(importId);
        Workbook workbook= null;
        try {

            workbook = WorkbookFactory.create(file.getInputStream());
            dImport.setTableNum(workbook.getNumberOfSheets()-1);
            for(int i=1;i<workbook.getNumberOfSheets();i++){
                Sheet sheet=workbook.getSheetAt(i);
                Row row1=sheet.getRow(0);
                Cell cell1=row1.getCell(1);
                Cell cell3=row1.getCell(3);
                DTable dTable=dTableMapper.selectDTableByCode(ExcelUtil.getCellValue(cell1),dImport.getProjectId());
                //删除表下面所有的列
                if(dTable!=null){
                    dColumnMapper.deleteAllColumnByProjectId(dImport.getProjectId(),dTable.getTableCode());
                    dTableIndexMapper.deleteAllIndexByProjectId(dImport.getProjectId(),dTable.getTableCode());
                    dTable.setTableName(cell3.getStringCellValue());
                    dTable.setUpdateName(dImport.getCreateName());
                    dTable.setUpdateDate(DateUtils.getNowDate());
                }else{
                    dTable=new DTable();
                    dTable.setDelFlag("0");
                    dTable.setProjectId(dImport.getProjectId());
                    dTable.setTableCode(cell1.getStringCellValue());
                    dTable.setTableName(cell3.getStringCellValue());
                    dTable.setCreateDate(DateUtils.getNowDate());
                    dTable.setCreateBy(dImport.getCreateBy());
                    dTable.setUpdateDate(DateUtils.getNowDate());
                    dTable.setUpdateBy(dImport.getUpdateBy());
                    dTable.setColumnNum(sheet.getLastRowNum()-2);
                    dTable.setdSort(new Long(0));
                    dTable.setCreateName(dImport.getCreateName());
                    dTable.setUpdateName(dImport.getCreateName());
                    dTableMapper.insertDTable(dTable);
                }

                DColumn dColumn=null;
                int columnCount=0;
                for(int j=2;j<sheet.getLastRowNum()+1;j++){
                    dColumn=new DColumn();
                    dColumn.setProjectId(dImport.getProjectId());
                    dColumn.setTableId(dTable.getId());
                    dColumn.setTableCode(dTable.getTableCode());
                    dColumn.setdSort(new Long(0));
                    dColumn.setCreateBy(dImport.getCreateBy());
                    dColumn.setCreateDate(DateUtils.getNowDate());
                    dColumn.setUpdateBy(dImport.getUpdateBy());
                    dColumn.setUpdateDate(DateUtils.getNowDate());
                    dColumn.setDelFlag("0");
                    Row row2=sheet.getRow(j);
                    Cell columnCell0=row2.getCell(0);
                    String columnCode=ExcelUtil.getCellValue(columnCell0);
                    if(!StringUtils.isNotNull(columnCode)){
                        continue;
                    }
                    dColumn.setColumnCode(columnCode);
                    Cell columnCell1=row2.getCell(1);
                    dColumn.setColumnName(ExcelUtil.getCellValue(columnCell1));
                    Cell columnCell2=row2.getCell(2);
                    dColumn.setDataType(ExcelUtil.getCellValue(columnCell2));
                    Cell columnCell3=row2.getCell(3);
                    dColumn.setdLength(ExcelUtil.getCellValue(columnCell3));
                    Cell columnCell4=row2.getCell(4);
                    String isEmpty=ExcelUtil.getCellValue(columnCell4);
                    if(StringUtils.isNotNull(isEmpty)&&isEmpty.equals("是")){
                        dColumn.setIsEmpty("1");
                    }else{
                        dColumn.setIsEmpty("0");
                    }
                    Cell columnCell5=row2.getCell(5);
                    String isPrimarykey=ExcelUtil.getCellValue(columnCell5);
                    if(StringUtils.isNotNull(isPrimarykey)&&isPrimarykey.equals("是")){
                        dColumn.setIsPrimarykey("1");
                    }else{
                        dColumn.setIsPrimarykey("0");
                    }
                    dColumnMapper.insertDColumn(dColumn);

                    columnCount++;
                }

                dTable.setColumnNum(columnCount);
                dTableMapper.updateDTable(dTable);
            }
            dImport.setInportStatus("1");

            DProject dProject=dProjectMapper.selectDProjectById(dImport.getProjectId());
            dProject.setTableNum(dTableMapper.selectTableNumByProjectId(dImport.getProjectId()));
            dProject.setUpdateDate(DateUtils.getNowDate());
            dProject.setUpdateName(dImport.getCreateName());
            dProjectMapper.updateDProject(dProject);

        } catch (Exception e) {
            e.printStackTrace();
            dImport.setInportStatus("2");
        }finally {
            dImportMapper.updateDImport(dImport);
            if(StringUtils.isNotNull(dImport.getInportStatus())&&dImport.getInportStatus().equals("2")){
                throw new CustomException("导入失败");//提示前端失败
            }
        }

    }
}

